import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/login/data/api/request_emailCode_api.dart';
import 'package:casdon/modules/login/data/api/valide_code_type.dart';
import 'package:casdon/modules/login/data/repository/auth_repository.dart';
import 'package:casdon/modules/login/domain/validators/input_validator.dart';

class SendEmailcodeUsecase {
  final AuthRepository repository;
  final InputValidator validator;

  const SendEmailcodeUsecase({required this.repository, required this.validator});

  Future<bool> execute(String email, ValideCodeType type) async {
    if (email.isEmpty) {
      throw "请先输入账号";
    }
    if (validator.isValude(email) == false) {
        throw "邮箱格式不正确";
    }
    try{
      RequestEmailcodeApi request = RequestEmailcodeApi(email: email, type: type.value);
      return await repository.sendEmailCode(request);
    }
    on BusinessException {
    rethrow; 
  } catch (e) {
    throw Exception("发送验证码失败");
  }
  }
}